﻿using Entity;
using Entity.SysEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CommandService.Implementation.Base
{
   public class Common
    {
     

       #region 数据表转成list模型
       /// <summary>
       /// 数据表转成list模型
       /// </summary>
       /// <typeparam name="T">目标模型</typeparam>
       /// <param name="table">数据源</param>
       /// <returns></returns>
       public virtual List<T> TableTolist<T>(DataTable table)
       {
           // 定义集合  
           List<T> list = new List<T>();

           // 获得此模型的类型  
           Type type = typeof(T);
           //定义一个临时变量  
           string tempName = string.Empty;
           //遍历DataTable中所有的数据行  
           foreach (DataRow dr in table.Rows)
           {
               T t = (T)Activator.CreateInstance(typeof(T));
               // 获得此模型的公共属性  
               PropertyInfo[] propertys = t.GetType().GetProperties();
               //遍历该对象的所有属性  
               foreach (PropertyInfo pi in propertys)
               {
                   tempName = pi.Name;//将属性名称赋值给临时变量  

                   if (table.Columns.Contains(tempName))//检查DataTable是否包含此列（列名==对象的属性名）    
                   {
                       // 判断此属性是否有Setter  
                       if (!pi.CanWrite) continue;//该属性不可写，直接跳出  
                       //取值  
                       object value = dr[tempName];
                       #region  //如果非空，则赋给对象的属性
                       if (value != DBNull.Value)
                       {
                           Type ty;
                           if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                           {
                               ty = Nullable.GetUnderlyingType(pi.PropertyType);
                           }
                           else
                           {
                               ty = pi.PropertyType;
                           }
                           if (ty.Name.Trim().IndexOf("INT")>-1)
                           {
                               pi.SetValue(t, Convert.ToInt32(value), null);
                           }
                           else
                           {
                               pi.SetValue(t, value.ToString(), null);
                           }
                       }
                       #endregion
                   }
               }
               //对象添加到泛型集合中  
               list.Add(t);
           }
           return list;

       }
       #endregion

        #region
       public virtual QueryOption EditTranslationMode(object obj)
       {
           QueryOption option = new QueryOption();
           var objectType = obj.GetType();
           var objectAttribute = (CustomAttribute)obj.GetType().GetCustomAttributes(true)[0];//获取模型对象自定义特性
           if (objectAttribute.TableName == null || objectAttribute.TableName.Trim().Equals(""))
           {
               return option;
           }
           else
           {
               option.TableName = objectAttribute.TableName;
           }
           PropertyInfo[] propertys = objectType.GetProperties();
           List<EntityStructure> list = new List<EntityStructure>();
           EntityStructure structsture;
           foreach (PropertyInfo pi in propertys)
           {
               var value = pi.GetValue(obj, null);
               if (value != null && !value.ToString().Trim().Equals(""))
               {
                   structsture = new EntityStructure();
                   structsture.FileName = pi.Name.Trim();
                   structsture.FileValue = value.ToString().Trim();
                   var fileAttribute = (FilesAttribute)pi.GetCustomAttributes(true)[0];//获取该字段的自定义特性
                   if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                   {
                       structsture.FileType = Nullable.GetUnderlyingType(pi.PropertyType).Name.ToString();
                   }
                   else
                   {
                       structsture.FileType = pi.PropertyType.Name;
                   }
                   structsture.Types = fileAttribute.DoType;
                   list.Add(structsture);
               }

           }
           option.QueryList = list;
           return option;
       }
        #endregion
    }
}
